/*
 * lea_utility.cc
 *
 *  Created on: Feb 10, 2014
 *      Author: jiang
 */
#include<stdint.h>
#include<stdlib.h>
#include<limits.h>
#include<vector>
#include"lea_types.h"


//Returns compact representation of certain region of the reference, from start_position,
//end at (not include) the end_position, without any jump.
uint8_t *get_reference_region(Position start_position,Position end_position,ReferenceInfo reference_info){
	Position i, pos,len = end_position - start_position;
	uint8_t *buf;
	buf = static_cast<uint8_t*>(calloc(len + 1, 1));
	for (i = 0; i < len; ++i) {
		pos = i + start_position;
		buf[i] = reference_info.pac[pos>>2] >> ((3 - (pos&3)) << 1) & 3;
		//fprintf(stderr,"%d,%d",buf[i],reference_info.pac[pos>>2]);
	}
	//fprintf(stderr,"\n");
	return buf;
}

static void combinations_helper(const std::vector<uint32_t> number_list,uint32_t offset,uint32_t k,Combination combination, CombinationList &combination_list){

	if(k == 0){
		/*
		for(uint i = 0; i < combination.size(); ++i){
			fprintf(stdout,"%d ",combination[i]);
		}
		fprintf(stdout,"\n");
		*/
		combination_list.push_back(combination);
	}

	for(uint i = offset; i < number_list.size(); ++i){
		combination.push_back(number_list[i]);
		combinations_helper(number_list,i+1,k-1,combination,combination_list);
		combination.pop_back();
	}
}
//Returns combinations, given number list and k
void combinations(Combination number_list,uint32_t k,CombinationList &combination_list)
{
	Combination combination;
	combinations_helper(number_list,0,k,combination,combination_list);
}


void debug_show_bit(uint8_t *buf, uint32_t len)
{
	uint32_t i;
	for(i = 0; i < len; ++i){
		fprintf(stderr,"%d",buf[i]);
	}
	fprintf(stderr,"\n");
}

char *int2bin(unsigned n, char *buf) {
#define BITS (sizeof(n) * CHAR_BIT)

	static char static_buf[BITS + 1];
	int i;

	if (buf == NULL)
		buf = static_buf;

	for (i = BITS - 1; i >= 0; --i) {
		buf[i] = (n & 1) ? '1' : '0';
		n >>= 1;
	}

	buf[BITS] = '\0';
	return buf;

#undef BITS
} ////
